#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Try to reproduce the bug #3913, at this time it hangs the Storage, but it doesn't
# trig the rewind error.
#
# This script uses the virtual disk autochanger
#
TestName="migration-job-bug-3913"
JobName=MigrationJobSave
. scripts/functions


scripts/cleanup
scripts/copy-migration-confs
echo "${cwd}/build" >${cwd}/tmp/file-list

change_jobname NightlySave $JobName
start_test

#
# Note, we first backup into Pool Default, 
#          then Migrate into Pool Full. 
#              Pool Default uses Storage=File
#              Pool Full    uses Storage=DiskChanger

# The trick is to comment out the Storage definition of the base Pool
$bperl -e "comment_out('$conf/bacula-dir.conf', 'Storage', 'Pool', 'Default')"

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'MigrationJobSave', 'MaxRunTime', '1min')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'migrate-job', 'MaxRunTime', '1min')"

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
setdebug level=000 storage=File
label storage=File volume=FileVolume001 Pool=Default
label storage=DiskChanger volume=ChangerVolume001 slot=1 Pool=Full drive=0
label storage=DiskChanger volume=ChangerVolume002 slot=2 Pool=Full drive=0
@# run two jobs (both will be migrated) from Default pool to Full pool
run job=$JobName yes
run job=$JobName yes
wait
messages
list jobs
list volumes
@#setdebug level=100 dir
@# should migrate two jobs
@#setdebug level=51 storage=DiskChanger
run job=migrate-job yes
@sleep 5
messages
@sleep 5
status dir
status storage=DiskChanger
messages
wait
@# purge volume=FileVolume001
list jobs 
list volumes
list joblog jobid=6
wait
@#
@# Now do another backup, but level Incremental
@#
run job=$JobName level=Incremental yes
wait
messages
@#
@# This final job that runs should be Incremental and
@# not upgraded to full.
list jobs
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
list volumes 
restore where=${cwd}/tmp/bacula-restores select storage=DiskChanger
unmark *
mark *
done
yes
list volumes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File
stop_bacula

check_two_logs
check_restore_diff
end_test
